import os
import ast
from conversion.transformers import ASTProcessor, ASTTransformer

SOURCE_PATH = "source_code/"
TARGET_PATH = "target_code/"
NET = "resnet50.py"


def traversal_project(folder_name):
    """
    dirpath是文件夹路径
    dirnames是文件夹名称
    filenames是文件名称
    :param folder_name:
    :return:
    """
    for dirpath, dirnames, filenames in os.walk(folder_name):
        for filename in filenames:
            if filename == '__init__.py':
                target_path = os.path.join('target_project', dirpath, filename)
                paths = os.path.split(target_path)
                for p in paths:
                    if not os.path.exists(p) and p[-3:] != '.py':
                        os.makedirs(p)
                with open(target_path, 'w', encoding='utf-8') as f:
                    pass
            source_path = os.path.join(dirpath, filename)
            target_path = os.path.join('target_net', dirpath, filename)
            processor = ASTProcessor(source_path, target_path)
            processor.print_ast()
            transformer = ASTTransformer()
            # transformer.top_node = processor.ast_root
            transformer.visit_Module(processor.ast_root)
            processor.print_ast()
            processor.write_target_code(processor.ast_to_code())



if __name__ == '__main__':
    traversal_project('source_net')